package com.hdax.dmw.item.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hdax.dmw.dtos.item.IndexNavDto;
import com.hdax.dmw.item.DmItemType;
import com.hdax.dmw.item.mappers.DmItemTypeMapper;
import com.hdax.dmw.item.mapstruct.IndexNavDtoMapper;
import com.hdax.dmw.item.service.IndexNavService;
import com.hdax.dmw.utils.Result;
import com.hdax.dmw.utils.ResultGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class IndexNavServiceImpl extends ServiceImpl<DmItemTypeMapper, DmItemType> implements IndexNavService {

    @Autowired
    private IndexNavDtoMapper dtoMapper;

    private List<IndexNavDto> navs(Integer parent){
        parent = parent == null?0:parent;
        List<DmItemType> navList = new ArrayList<>();
        QueryWrapper<DmItemType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent",parent);
        navList = list(queryWrapper);
        List<IndexNavDto> dtos = dtoMapper.toTargetList(navList);
        dtos.forEach(dto->{
            dto.setChildren(navs(Math.toIntExact(dto.getId())));
        });
        return dtos;
    }

    @Override
    public Result<List<IndexNavDto>> indexNavList() {
        return ResultGenerator.genSuccessResult(navs(null));
    }
}
